package com.liwei.baselib.utils;

import android.content.Context;
import android.media.MediaPlayer;

/**
 * Created by Administrator on 2018/1/22.
 */

public class ToolPlayer {
    private static MediaPlayer mp = new MediaPlayer();


    public static void getMediaPayer(Context context, int rawid) {
//        AssetFileDescriptor file = context.getResources().openRawResourceFd(rawid);
//        try {
//            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//            mp.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
//                    file.getLength());
        mp = MediaPlayer.create(context, rawid);
//            file.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        mp.setVolume(0.5f, 0.5f);
        mp.setLooping(false);// 关闭循环播放
        mp.start();

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mp.release();
            }
        });


    }

    public static void stopMediaPayer() {
        if (mp != null) {
            mp.release();
        }
    }


}
